function setUp(){
	
	PicBoard.getTranslations(['lang_237_confirmDeleteGroup'], function(){});
			
	$('#controlArea').css({
		'margin-top': '27px',
		'height': $('#UserResultArea').height() 
	});

	// Bind buttons
	
	$('#saveEdits').bind('click', function(){
		// Clicking the save button should send the gID, group name and description to the server and wait for a response.
		$.post(PicBoard.baseURL + "rsrc.php?req=updateGroup", { gID: $('input#gID').val(), name: $('input#groupName').val(), description: $('textarea#groupDescription').val() }, function(data){
			if(data.statusCode == 200){
				alert(data.message);
			} else {
				alert(data.message);
			}
		}, 'json');			
		
	});
	
	$('#deleteGroup').bind('click', function(){
		$.post(PicBoard.baseURL + "rsrc.php?req=deleteGroupConfirmation", { gID: $('input#gID').val() }, function(data){
			if(data.statusCode == 200){
				var baseMessage = PicBoard.languageMap.lang_237_confirmDeleteGroup;
				
				baseMessage += "\n\nThe following resources will be removed: ";
				
				if(data.payload.charts.length > 0){
					baseMessage += "\nCharts: ";
					
					for(var c in data.payload.charts){
						baseMessage += data.payload.charts[c].name + ", ";
					}
					
					baseMessage = baseMessage.substring(0, baseMessage.length - 2);
				}
				
				if(data.payload.books.length > 0){
					baseMessage += "\nBooks: ";
	
					for(var c in data.payload.books){
						baseMessage += data.payload.books[c].name + ", ";
					}		
	
					baseMessage = baseMessage.substring(0, baseMessage.length - 2);
				}
				
				if(data.payload.albums.length > 0){
					baseMessage += "\Albums: ";				
	
					for(var c in data.payload.albums){
						baseMessage += data.payload.albums[c].name + ", ";
					}		
	
					baseMessage = baseMessage.substring(0, baseMessage.length - 2);
				}
				
				if(data.payload.photos.length > 0){
					baseMessage += "\Photos: ";	
						
					for(var c in data.payload.photos){
						baseMessage += data.payload.photos[c].name + ", ";
					}		
	
					baseMessage = baseMessage.substring(0, baseMessage.length - 2);
				}
				
				confirmation = confirm(baseMessage);
				
				if(confirmation){
					// User has accepted delete. Send delete request to the server.
					$.post(PicBoard.baseURL + 'rsrc.php?req=removeGroup', { 'id': $('input#gID').val() }, function(data){
						alert(data.message);
					});
				}
			} else {
				alert(data.message);
			}
		}, 'json');	
	});
	
	$('#addUsers').bind('click', function(){ addUserToGroup() });
	
	// Accept and delete user requests
	$('.acceptUser').bind('click', function(){
		var link = this;
		$.post(PicBoard.baseURL + 'rsrc.php?req=acceptGroupJoinRequest', { 'gID': $(this).children('input[name=groupID]').val(), 'uID': $(this).children('input[name=userID]').val() }, function(data){
			if(data.statusCode == 200){
				$(link).parent().parent().remove();
			} else {
				alert(data.message);
			}
		}, 'json');	
	});
	
	$('.denyUser').bind('click', function(){
		var link = this;
		$.post(PicBoard.baseURL + 'rsrc.php?req=denyGroupJoinRequest', { 'gID': $(this).children('input[name=groupID]').val(), 'uID': $(this).children('input[name=userID]').val() }, function(data){
			if(data.statusCode == 200){
				$(link).parent().parent().remove();
			} else {
				alert(data.message);
			}
		}, 'json');	
	});
	
	$('#groupSelector').bind('change', function(){
		window.location = "managegroup?id=" + $(this).val();
	});
}




function addUserToGroup(){
        getFragment("inviteUsers", function(data){
                jQuery.facebox(data);
                
                idFrag = document.location.toString().match(/(\d+)$/);
                var id = idFrag[1];
                   
                        
                // Bind any remove buttons that we create to remove that row.
                $('.removeUserButton').live('click', function(){
                        $(this).parent().parent().remove();
                });
                
                
                // Bind the add button to add a new row.
                $('#PBButtonAddInstUsers').bind('click', function(){
                        var thisButton = $(this);
                        $.post(PicBoard.baseURL + "rsrc.php?req=validateUsername", { 'username': $('#addEmail').val() }, function(data){
                                        
                            if(data.statusCode == 200){
                                    newEls = $(thisButton).parent().parent().clone();

                                    newEls.find('input[id=addEmail]').replaceWith( newEls.find('input[id=addEmail]').val() );
                                    newEls.find('input[id=isAdmin]').replaceWith( (newEls.find('input[id=isAdmin]:checked').val() == null) ? "no" : "yes" );
                                    newEls.find('button[id=PBButtonAddInstUsers]').text('Remove').attr( {'id':'', 'class': 'removeUserButton'} );
                                    
                                    $(thisButton).parent().parent().before(newEls);
                                    $('button').button();
                                    
                                    $('#addEmail').val("");
                                    $('#addEmail').focus();
                            } else {
                                    alert("This user does not exist in PicBoard.");
                            }
                                        
                       }, 'json');


                });
                
                // Confirm the added users and send to the server.                
                $('#PBButtonConfirmInstUsers').bind('click', function(){
                        var sendObject = { 'id': id, 'data':[] };
                        
                        // Pull out the information from the table
                        $('#addUsers > table > tbody > tr:gt(0)').each(function(){
                                sendObject.data.push( { email: $(this).children("td:eq(0)").html(), admin: $(this).children("td:eq(1)").html() } );
                        });
                        
                        sendObject.data.splice(sendObject.data.length -1, 1);
                        
                        jQuery.facebox("<div style='text-align:center; margin:5px'><img src='" + PicBoard.baseURL + "images/loading_fb.gif" + "' /><br />Applying changes...</div>");
                        
                        $.post(PicBoard.baseURL + "rsrc.php?req=inviteUsersGroup", sendObject, function(data){
                                jQuery.facebox(data.message);
                                
                                setTimeout(function(){
                                        jQuery.facebox(data.message);
                                        setTimeout(function(){ jQuery.facebox.close(); }, 1500)
                                }, 1500);
                        }, 'json');
                        
                });
        });
}


PicBoard.addLoadEvent( "setUp()" );